import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex);

// 定义一个容器

let selectModule={
  state:{
    title:'hello',
    list:[]
  },
  mutations:{
    changeTitle(state,payload){
      state.title = payload.title;
    },
    changeList(state,payload){
      state.list = payload.list
    }

  },
  actions:{
    getListAction(context){
      //发送请求
      axios.get('https://easy-mock.com/mock/5b6ede13640612182aa07d5a/list')
        .then((data)=>{
          console.log(data.data)
          context.commit('changeList',{list:data.data})
        }).catch((error)=>{
        console.log(error)
      })
    }
  }

}

let store = new Vuex.Store({
  // 定义状态
  state:{
    count:100,
    title:'hello',
    list:[]
  },
  getters:{
   filterCount(state){
     return state.count>=120?120:state.count
   }
  },
  mutations:{
    addIncrement(state,payload){

      state.count+=payload.n;
    },
    reduceDecrement(state,payload){
      state.count-=payload.de;
    }

  },
  actions:{
    addAction(context){
      console.log(context)
      setTimeout(()=>{
        // 改变状态, 提交mutations
        context.commit('addIncrement',{n:5})
        context.dispatch("textAction",{test:'测试'})
      },1000)
    },
    textAction(context,obj){
      console.log('textAction被触发了>>>>obj.test >>>'+obj.test)
    }

  },
  modules:{selectModule}
});


export default store
